﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Hosting;
using YFIN.Data;
using YFIN.Helper.Extension;
using YFIN.Models;

namespace YFIN.Service.User
{
    public class UserService : IUserService
    {
        /// <summary>
        /// 添加一个用户
        /// </summary>
        /// <param name="user">要添加的用户</param>
        public bool Add(ModelUser user)
        {
            try
            {
                YFINExtension.GoAndSave(entities => entities.YFIN_User.AddObject(ModelUser.ConvertToYFIN_User(user)));
                var userFile = HostingEnvironment.MapPath("~/UserData") + "/" + user.UserName;
                if(!Directory.Exists(userFile))
                {
                    Directory.CreateDirectory(userFile);
                }
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 通过用户名查找用户
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>找到的用户</returns>
        public ModelUser GetByUserName(string userName)
        {
            return ModelUser.ConvertToModelUser(YFINExtension.Go(
                entities => entities.YFIN_User.SingleOrDefault(u => u.UserName == userName)));
        }

        /// <summary>
        /// 查找所有用户
        /// </summary>
        /// <returns>找到所有用户</returns>
        public List<ModelUser> GetAll()
        {
            return YFINExtension.Go(entities => entities.YFIN_User.Where(u=>u.UserName != "#YFIN#").ToList()).Select(ModelUser.ConvertToModelUser).ToList();       
        }

        /// <summary>
        /// 判读是否与给定用户名密码相匹配的用户
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>是否存在匹配</returns>
        public ModelUser Exist(string userName, string password)
        {
            return ModelUser.ConvertToModelUser(YFINExtension.Go(
                    entities => entities.YFIN_User.SingleOrDefault(
                        u => u.UserName == userName && u.Password == password)));
        }

        /// <summary>
        /// 获取用户文件路径
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>文件路径</returns>
        public string GetPath(string userName)
        {
            return HostingEnvironment.MapPath("~/UserData") + "/" + userName;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="newUser">更新后的用户</param>
        /// <returns>是否成功</returns>
        public bool Update(ModelUser newUser)
        {
            try
            {
                YFINExtension.GoAndSave(
                    entities =>
                        {
                            var user = entities.YFIN_User.SingleOrDefault(u => u.UserName == newUser.UserName);
                            ModelUser.CopyToYFIN_User(user, newUser);
                        });
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="user">要删除的用户</param>
        /// <returns>是否成功</returns>
        public bool Delete(ModelUser user)
        {
            try
            {
                var bas = new Lockbox.BankAccountService();
                var ba = bas.GetByUserName(user.UserName);
                foreach (var b in ba)
                {
                    bas.Delete(b);
                }
                var fs = new Lockbox.FileService();
                var files = fs.GetByUserName(user.UserName);
                foreach (var f in files)
                {
                    fs.Delete(f);
                }
                var msgs = new MessageService();
                var ms = msgs.GetByReceiver(user.UserName);
                foreach (var m in ms)
                {
                    msgs.Delete(m);
                }
                var rps = new Toolkit.RiskPreferService();
                var rp = rps.GetByUserName(user.UserName);
                foreach (var r in rp)
                {
                    rps.Delete(r);
                }
                var rms = new Lockbox.ReminderService();
                var rs = rms.GetByUserName(user.UserName);
                foreach (var r in rs)
                {
                    rms.Delete(r);
                }
                var pts = new Toolkit.PropertyService();
                var pt = pts.GetTypeByUserName(user.UserName);
                foreach (var p in pt)
                {
                    pts.DeleteType(p);
                }
                YFINExtension.GoAndSave(entities =>
                                        entities.YFIN_User.DeleteObject(
                                            entities.YFIN_User.SingleOrDefault(u => u.UserName == user.UserName)));
                var userFile = HostingEnvironment.MapPath("~/UserData") + "/" + user.UserName;
                if (Directory.Exists(userFile))
                {
                    Directory.Delete(userFile, true);
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
